#pragma once
#include <iostream>
#include <string>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "log.hpp"

class Util
{
public:
    static int ReadOneLine(int fd, std::string& out)
    {
        char ch = 0;
        while(ch != '\n')
        {
            ssize_t n = recv(fd, &ch, 1, 0);
            if(n <= 0)
            {
                Log(ERROR, "read error");
                return -1;
            }
            if(ch == '\r')
            {
                recv(fd, &ch, 1, MSG_PEEK);
                if(ch == '\n')
                {
                    recv(fd, &ch, 1, 0);
                }
                else
                {
                    ch = '\n';
                }
            }
            out += ch;
        }
	    out.pop_back();
        return out.size();
    }
};
